package com.bw.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.core.result.ResponseResult;
import com.bw.user.mapper.TbUserMapper;
import com.bw.user.pojo.TbBankCardAuth;
import com.bw.user.pojo.TbOperatorAuth;
import com.bw.user.mapper.TbOperatorAuthMapper;
import com.bw.user.pojo.TbUser;
import com.bw.user.service.TbOperatorAuthService;
import com.bw.user.vo.OperatorJson;
import com.bw.user.vo.OperatorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 运营商认证表 服务实现类
 * </p>
 *
 * @author lv
 * @since 2021-03-23
 */
@Service
public class TbOperatorAuthServiceImpl extends ServiceImpl<TbOperatorAuthMapper, TbOperatorAuth> implements TbOperatorAuthService {

    @Autowired
    private TbOperatorAuthMapper operatorAuthMapper;

    @Autowired
    private TbUserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ResponseResult operator(OperatorVo operatorVo) {

        if (StrUtil.isEmpty(operatorVo.getPhone())) {
            return ResponseResult.error("手机号不能为空");
        }
        if (StrUtil.isEmpty(operatorVo.getYzm())) {
            return ResponseResult.error("验证码不能为空");
        }

        QueryWrapper<TbOperatorAuth> wrapper = new QueryWrapper<>();
        wrapper.eq("user_phone", operatorVo.getPhone());
        List<TbOperatorAuth> operatorAuths = operatorAuthMapper.selectList(wrapper);

        if (CollUtil.isNotEmpty(operatorAuths)) {
            return ResponseResult.error("已经认证过");
        }
        String s = (String) redisTemplate.opsForValue().get(operatorVo.getPhone());
        if (!s.equals(operatorVo.getYzm())) {
            return ResponseResult.error("您输入的验证码不正确!");
        }

        HashMap<String, Object> operatorMap = new HashMap<>();
        operatorMap.put("mobile", operatorVo.getPhone());
        operatorMap.put("code", operatorVo.getYzm());
        String post = HttpUtil.post("http://localhost:8080/api/common/mobileAuth", operatorMap);

        OperatorJson operatorJson = JSONObject.parseObject(post, OperatorJson.class);
        TbOperatorAuth tbOperatorAuth = new TbOperatorAuth();

        tbOperatorAuth.setCreateTime(new Date());
        tbOperatorAuth.setIsDelete(0);
        tbOperatorAuth.setOperatorData(JSONObject.toJSONString(operatorJson.getData().getRes_data()));
        tbOperatorAuth.setOperatorCode(operatorJson.getMessage());
        tbOperatorAuth.setUpdateTime(new Date());
        tbOperatorAuth.setUserPhone(operatorVo.getPhone());
        tbOperatorAuth.setUserId(operatorVo.getUserid());
        operatorAuthMapper.insert(tbOperatorAuth);
        //redisTemplate.delete(operatorVo.getPhone());
        return new ResponseResult(200, "您的运营商认证成功!");
    }
}
